package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.OrderDao;
import com.example.librarysystem.entity.Order;
import com.example.librarysystem.entity.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderDao orderDao;

    @Override
    public Integer selectCount(){
        return orderDao.selectCount();
    }

    @Override
    public List<OrderInfo> selectOrderInfo(int borrowerId, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return orderDao.selectOrderInfo(borrowerId, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<OrderInfo> selectOrderInfoSearch(int borrowerId, String bookName, String author, String publishingHouse, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return orderDao.selectOrderInfoSearch(borrowerId,bookName,author,publishingHouse,startCurrentPage,pageSize);
        }
    }
    @Override
    @Transactional(timeout = 30)
    public int deleteOrderById(int id){
        orderDao.deleteOrderById(id);
        return 0;
    }
    @Override
    public List<Order> selectByBookName(String bookName){
        return orderDao.selectByBookName(bookName);
    }
    @Override
    public List<Order> selectByBookIdAndEndDate(int bookId, Date endDate){
        return orderDao.selectByBookIdAndEndStart(bookId, endDate);
    }
    @Override
    @Transactional(timeout = 30)
    public int insertOrder(Order order){
        orderDao.insertOrder(order);
        return 0;
    }
    @Override
    public int selectOrderInfoCount(int borrowerId){
        return orderDao.selectOrderInfoCount(borrowerId);
    }
    @Override
    public int selectOrderInfoSearchCount(int borrowerId, String bookName, String author, String publishingHouse){
        return orderDao.selectOrderInfoSearchCount(borrowerId,bookName,author,publishingHouse);
    }
    @Override
    public List<Order> selectAll(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return orderDao.selectAll(startCurrentPage, pageSize);
        }
    }
    @Override
    public Order selectOrderById(int id){
        return orderDao.selectOrderById(id);
    }
    @Override
    public List<Order> selectOrderBetween(Date start, Date end){
        return orderDao.selectOrderBetween(start, end);
    }
    @Override
    public List<Order> selectOrderOver(Date end){
        return orderDao.selectOrderOver(end);
    }
}
